package com.tongda.core.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;

import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tongda.core.common.Constants;
import com.tongda.core.enums.StatusCodeEnum;
import com.tongda.core.utils.CommunicationUtil;
import com.tongda.core.vo.Dish;
import com.tongda.core.vo.DwzAjaxDone;
import com.tongda.core.vo.Employee;
import com.tongda.core.vo.MessageVO;
import com.tongda.core.vo.MobileRspResult;
import com.tongda.core.vo.SelfOrder;
import com.tongda.core.vo.Table;
import com.tongda.core.vo.TableArea;
import com.tongda.core.vo.TableOrder;

public class TableService extends BaseService {

	public static List<Table> getRestTableList(Map<String, Object> uriVariables) {
		ResponseEntity<MobileRspResult> responseEntity = BaseService.get(
				"mobile/table/list", MobileRspResult.class, uriVariables);
		if (responseEntity != null) {
			MobileRspResult r = responseEntity.getBody();
			if (r.getStatusCode().equals(StatusCodeEnum.SUCCESS.getCode())) {
				return Arrays.asList(parseObjectToT(r.getObject(),
						Table[].class));
			}
		}
		return new ArrayList<Table>();
	}

	public static List<Table> getAllTables() {
		Map<String, Object> params = new WeakHashMap<String, Object>();
		return getRestTableList(params);
	}

	public static List<Table> getTableById(String tabId) {
		Map<String, Object> params = new WeakHashMap<String, Object>();
		params.put("search_EQ_tabId", tabId);
		return getRestTableList(params);
	}

	public static List<Table> getFreeTable() {
		Map<String, Object> params = new WeakHashMap<String, Object>();
		params.put("search_EQ_dinnerStatus", 1);
		return getRestTableList(params);
	}

	public static List<Table> getInUsedTable(String tabId) {
		Map<String, Object> params = new WeakHashMap<String, Object>();
		params.put("search_EQ_dinnerStatus", 2);
		params.put("search_NEQ_tabId", tabId);
		return getRestTableList(params);
	}

	// 预定
	public static List<TableOrder> getTableOrderList(Map<String, ?> uriVariables) {
		ResponseEntity<TableOrder[]> responseEntity = BaseService.get(
				"mobile/bill/getTableOrderList", TableOrder[].class,
				uriVariables);
		if (responseEntity != null) {
			return Arrays.asList(responseEntity.getBody());
		}
		return null;
	}

	/**
	 * @return
	 * 
	 */
	public static MobileRspResult openTable(Object obj) {
		ResponseEntity<MobileRspResult> responseEntity = BaseService
				.postEntity("mobile/table/openTable", obj.getClass(), obj,
						MobileRspResult.class);
		if (responseEntity != null) {
			return responseEntity.getBody();
		}
		return new MobileRspResult(StatusCodeEnum.ERROR.getCode(), "失败", "");
	}

	public static MobileRspResult openTableUpdate(Object obj) {
		ResponseEntity<MobileRspResult> responseEntity = BaseService
				.postEntity("mobile/table/openTableUpdate", obj.getClass(),
						obj, MobileRspResult.class);
		if (responseEntity != null) {
			return responseEntity.getBody();
		}
		return new MobileRspResult(StatusCodeEnum.ERROR.getCode(), "失败", "");
	}

	/**
	 * 获取所有的营销员
	 */
	public static List<Employee> getSalesMan() {
		ResponseEntity<MobileRspResult> responseEntity = BaseService.get(
				"mobile/table/getSalesMan", MobileRspResult.class, null);
		if (responseEntity != null) {
			MobileRspResult r = responseEntity.getBody();
			if (r.getStatusCode().equals(StatusCodeEnum.SUCCESS.getCode())) {
				Employee[] ls = parseObjectToT(r.getObject(), Employee[].class);
				if (ls == null) {
					return new ArrayList<Employee>();
				}
				return Arrays.asList(ls);
			}
		}
		return new ArrayList<Employee>();
	}

	public static MobileRspResult transmitTable(Object obj) {
		ResponseEntity<MobileRspResult> responseEntity = BaseService
				.postEntity("mobile/table/transmitTable", obj.getClass(), obj,
						MobileRspResult.class);
		if (responseEntity != null) {
			return responseEntity.getBody();
		}
		return new MobileRspResult(StatusCodeEnum.ERROR.getCode(), "失败", "");
	}

	public static MobileRspResult combineTable(Object obj) {
		ResponseEntity<MobileRspResult> responseEntity = BaseService
				.postEntity("mobile/table/combineTable", obj.getClass(), obj,
						MobileRspResult.class);
		if (responseEntity != null) {
			return responseEntity.getBody();
		}
		return new MobileRspResult(StatusCodeEnum.ERROR.getCode(), "失败", "");
	}

	public static MobileRspResult cleanTable(String tabId) {
		MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
		map.add("id", tabId);
		ResponseEntity<MobileRspResult> responseEntity = BaseService.post(
				"mobile/table/cleanTable", map, MobileRspResult.class);
		if (responseEntity != null) {
			return responseEntity.getBody();
		}
		return new MobileRspResult(StatusCodeEnum.ERROR.getCode(), "失败", "");
	}

	public static String[] openSelfTable(String tabNo, String id) {
		MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
		map.add("tabNo", tabNo);
		map.add("id", id);
		System.out.println("tabNo:" + tabNo);
		return BaseService.post("mobile/table/handleSelfTable", map,
				String[].class).getBody();
	}

	public static boolean handleSelfCall(String tabNo, String callType,
			String id) {
		MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
		map.add("tabNo", tabNo);
		map.add("callType", callType);
		map.add("id", id);
		return BaseService.post("mobile/table/handleSelfCall", map,
				Boolean.class).getBody();
	}

	/**
	 * 自助单信息
	 * 
	 * @param uriVariables
	 * @return
	 */
	public static SelfOrder getOrderDish(Map<String, ?> uriVariables) {
		ObjectMapper objectMapper = new ObjectMapper();
		String suffix = "?";
		if (uriVariables != null) {
			Set<String> keys = uriVariables.keySet();
			for (String key : keys) {
				Object value = uriVariables.get(key);
				suffix += key + "=" + value + "&";
			}
		}
		String jsonStr = CommunicationUtil.get(Constants.BASE_SERVIER_URL
				+ "self/getDishList" + suffix);
		SelfOrder selfOrder = null;
		Dish[] arr;
		try {
			selfOrder = objectMapper.readValue(jsonStr, SelfOrder.class);
			if (selfOrder.getDishesStr() != null
					&& selfOrder.getDishesStr() != "") {
				arr = objectMapper.readValue(selfOrder.getDishesStr(),
						Dish[].class);
				ArrayList<Dish> list = new ArrayList<Dish>();
				for (Dish dish : arr) {
					list.add(dish);
				}
				selfOrder.setDishes(list);
			}
			return selfOrder;
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return selfOrder;
	}

	public static List<MessageVO> getServiceHistory() {
		Map<String, Object> params = new WeakHashMap<String, Object>();
		params.put("search_EQ_restId", Constants.RESTAURANT_ID);
		params.put("search_EQ_mesType", 0);
		params.put("search_EQ_status", 1);

		ResponseEntity<MessageVO[]> responseEntity = BaseService.get(
				"self/getSelfCallList", MessageVO[].class, params);
		if (responseEntity != null) {
			return Arrays.asList(responseEntity.getBody());
		}
		return null;
	}

	public static List<MessageVO> getCallHistory() {
		Map<String, Object> params = new WeakHashMap<String, Object>();
		params.put("search_EQ_restId", Constants.RESTAURANT_ID);
		params.put("search_NEQ_mesType", 0);
		params.put("search_EQ_status", 1);
		params.put("sortType", "updateTime_desc");

		ResponseEntity<MessageVO[]> responseEntity = BaseService.get(
				"self/getSelfCallList", MessageVO[].class, params);
		if (responseEntity != null) {
			return Arrays.asList(responseEntity.getBody());
		}
		return null;
	}

	public static DwzAjaxDone checkBillDishInStock(String tabNo, String id) {
		MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
		map.add("tabNo", tabNo);
		// map.add("id", id);
		return BaseService.post("mobile/table/checkBillDishInStock/" + id, map,
				DwzAjaxDone.class).getBody();
	}

	/**
	 * 获取所有的餐台区域
	 */
	public static List<TableArea> getAllTableAreas() {
		ResponseEntity<MobileRspResult> responseEntity = BaseService.get(
				"mobile/table/getAllTableAreas", MobileRspResult.class, null);
		if (responseEntity != null) {
			MobileRspResult r = responseEntity.getBody();
			if (r.getStatusCode().equals(StatusCodeEnum.SUCCESS.getCode())) {
				TableArea[] tableAreas = parseObjectToT(r.getObject(),
						TableArea[].class);
				if (tableAreas != null) {
					return Arrays.asList(tableAreas);
				}
			}
		}
		return new ArrayList<TableArea>();
	}

	/**
	 * 根据餐台的Id获取餐台的状态
	 * 
	 * @param tableId
	 */
	public static String getDinnerStatus(String tableId) {
		Map<String, Object> map = new WeakHashMap<String, Object>();
		map.put("tableId", tableId);
		ResponseEntity<MobileRspResult> responseEntity = BaseService.get(
				"mobile/table/getDinnerStatus", MobileRspResult.class, map);
		if (responseEntity != null) {
			MobileRspResult r = responseEntity.getBody();
			if (r.getStatusCode().equals(StatusCodeEnum.SUCCESS.getCode())) {
				return String.valueOf(r.getObject());
			}
		}
		return "";
	}

	/**
	 * 呼叫服务,呼叫下单
	 */
	public static String callService(String tabNO, String type, String content) {
		Map<Object, Object> params = new WeakHashMap<Object, Object>();
		params.put("tabNO", tabNO);
		params.put("type", type);
		params.put("content", content);
		String result = CommunicationUtil.post(Constants.BASE_SERVIER_URL
				+ "mobile/table/open/callwaiterByMenu", params);
		if(result!=null){
			return result;
		}
		return StatusCodeEnum.ERROR.getCode();
	}
	
	/**
	 *  判断账单是否包含（在线支付、微信刷卡、微信公众号）三种支付方式
	 * @param billId 账单ID
	 * @return
	 */
	public static Boolean getDinerBillPayment(String billId) {
		MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
		map.add("billId", billId);
		return BaseService.post("mobile/table/getDinerBillPayment", map,
				Boolean.class).getBody();
	}
}
